<template>
  <div class="student-salary-container">
    <el-card>
      <div class="header-title">我的工资管理</div>
      <el-table :data="tableData" border stripe style="width: 100%; margin-top: 20px;">
        <el-table-column prop="positionName" label="岗位名称" align="center" />
        <el-table-column prop="salary" label="薪资(元/小时)" align="center" />
        <el-table-column prop="totalHours" label="总工时" align="center">
          <template #default="scope">
            <span>{{ scope.row.totalHours }} 小时</span>
          </template>
        </el-table-column>
        <el-table-column prop="totalSalary" label="总工资(元)" align="center">
          <template #default="scope">
            <span>{{ scope.row.totalSalary }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-wrapper">
        <el-pagination
          background
          layout="prev, pager, next, jumper, ->, total"
          :total="total"
          :page-size="pageSize"
          :current-page="pageNum"
          @current-change="handlePageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { getStudentSalaryPage } from '@/api/salary';

const tableData = ref([]);
const total = ref(0);
const pageNum = ref(1);
const pageSize = ref(8);
const loading = ref(false);

const fetchData = async () => {
  loading.value = true;
  try {
    const res = await getStudentSalaryPage({
      page: pageNum.value,
      size: pageSize.value
    });
    if (res && res.code === 0) {
      tableData.value = res.data.records.map(item => ({
        positionName: item.positionName,
        salary: item.positionSalary,
        totalHours: item.totalHour,
        totalSalary: item.totalSalary,
        status: item.status
      }));
      // 狀態排序：未發放(2) > 未审核(0) > 已發放(1)
      tableData.value.sort((a, b) => {
        const order = {2: 0, 0: 1, 1: 2}
        return order[a.status] - order[b.status]
      })
      total.value = res.data.total;
    } else {
      ElMessage.error(res.message || '加载失败');
    }
  } catch (e) {
    console.error('Salary fetch error:', e);
    if (e.response) {
      ElMessage.error(`请求失败: ${e.response.status} - ${e.response.statusText}`);
    } else if (e.request) {
      ElMessage.error('网络连接失败，请检查网络连接');
    } else {
      ElMessage.error('请求失败: ' + e.message);
    }
  } finally {
    loading.value = false;
  }
};

const handlePageChange = (val) => {
  pageNum.value = val;
  fetchData();
};

function getStatusText(status) {
  switch (status) {
    case 0: return '未审核';
    case 1: return '已发放';
    case 2: return '未发放';
    default: return '未知';
  }
}
function getStatusType(status) {
  switch (status) {
    case 0: return 'warning';
    case 1: return 'success';
    case 2: return 'danger';
    default: return 'info';
  }
}

onMounted(() => {
  fetchData();
});
</script>

<style scoped>
.student-salary-container {
  padding: 24px;
}
.header-title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 10px;
  color: #222;
}
.pagination-wrapper {
  margin-top: 20px;
  text-align: right;
}
</style> 